package br.hugonardo.twitter.timeline;

import java.util.List;

import twitter4j.Status;
import twitter4j.TwitterException;
import android.app.ListActivity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
import br.hugonardo.twitter.R;
import br.hugonardo.twitter.helper.Constants;
import br.hugonardo.twitter.helper.TwitterService;
import br.hugonardo.twitter.updatestatus.ComposeUpdateStatusActivity;

public class TimelineActivity extends ListActivity {

	private final TwitterService twitterService = TwitterService.getInstance();
	
	@Override
	protected void onResume() {
		super.onResume();
		
		try {
			List<Status> timeline = this.twitterService.getTimeline();
			setListAdapter(new TweetAdapter<Status>(this, R.layout.timeline, timeline));
		} catch (TwitterException e) {
			Toast.makeText(this, "Houve um erro ao obter a timeline via API", Toast.LENGTH_LONG).show();
			Log.e(Constants.LOG_CAT_CATEGORY, "Houve um erro ao obter a timeline via API", e);
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.timeline_menu, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.compose_update:
			this.openComposeUpdateActivity();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	private void openComposeUpdateActivity() {
		Intent intent = new Intent().setClass(this, ComposeUpdateStatusActivity.class);
		startActivity(intent);
	}
}
